#include "GItems.h"

LPDIRECT3DTEXTURE9 CItem::_itemImage;

CItem::CItem(int left,int top,int kind,int hidden,QuadTree *q,SOUND *sManager)
{
	_typeObj=0;
	_layer=1;	
	_posOld=0;
	_check=true;
	_quadTree=q;
	_pRECT->left=left;
	_pRECT->top=top;
	_hidden=hidden;
	_kind=kind;
	_isActive=true;
	_id=countObj++;
	_status=0;//Normal
	_pSoundManager=sManager;
	switch(_kind)
	{
	case 0://grass
		_hidden=-1;
		_pRECT->right=_pRECT->left+GRASS_W;
		_pRECT->bottom=_pRECT->top+GRASS_H;
		_pRECT->left=left;
		_pSprite=new CSprites(_itemImage,true,hidden,0,70,ITEMS_W,GRASS_H,left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 1://brick
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		_amountCoin=0;
		_pSprite=new CSprites(_itemImage,true,0,1,0,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,0);
		break;
	case 2://question
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		if(_hidden==0)
			_amountCoin=1;
		else
			_amountCoin=0;
		_pSprite=new CSprites(_itemImage,true,0,3,ITEMS_H,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,300);
		break;
	case 3://brick have alot of coin
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		_amountCoin=10;
		_pSprite=new CSprites(_itemImage,true,0,1,0,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,50);
		break;
	case 4://pipe long
		_pRECT->right=_pRECT->left+PIPE_W;
		_pRECT->bottom=_pRECT->top+PIPE_H;
		_pSprite=new CSprites(_itemImage,true,0,0,150,PIPE_W,PIPE_H,left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 5:// stone
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		_pSprite=new CSprites(_itemImage,false,1,1,0,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 6://hard brick
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		_pSprite=new CSprites(_itemImage,false,2,1,0,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 7://special brick
		_pRECT->right=_pRECT->left+ITEMS_W;
		_pRECT->bottom=_pRECT->top+ITEMS_H;
		_pSprite=new CSprites(_itemImage,false,3,1,0,ITEMS_W,ITEMS_H,_pRECT->left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 8://pipe small
		_pRECT->right=_pRECT->left+PIPE_W;
		_pRECT->bottom=_pRECT->top+PIPE_M_H;
		_pSprite=new CSprites(_itemImage,true,0,0,150,PIPE_W,PIPE_M_H,left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	case 9://pipe special
		_pRECT->right=_pRECT->left+PIPE_W;
		_pRECT->bottom=_pRECT->top+200;
		_pSprite=new CSprites(_itemImage,true,2,0,70,PIPE_W,200,left,_pRECT->bottom,0,0);
		_amountCoin=0;
		break;
	}
}

void CItem::Collision(Object *obj)
{
	if(obj->_typeObj==1)//My Mario
	{
		Object *objTemp=NULL;
		
		switch(_kind)
		{
		case 1://break brick
			if(((Mario*)obj)->_level!=0)
			{
				_pSoundManager->Play("BrickBroken",false);
				_effect->Add(_pRECT->left,_pRECT->top,0);
				_pSprite->_totalExistTime=1;
				_status=2;
			}
			break;
		case 2://question
			_pSprite->_count=0;
			_pSprite->_index=1;
			_pSprite->_row=0;
			_pSprite->_interval=0;
			switch(_hidden)
			{
			case 0://coin
				_pSoundManager->Play("GetCoin",true);
				_effect->Add(_pRECT->left,_pRECT->top-ITEMS_H-16,1);
				info->CointCounter++;
				break;
			case 1://healthy Mushroom or Flower
				_pSoundManager->Play("BonusAppear_LevelDown",false);
				if(((Mario*)obj)->_level==0)
				{
					objTemp=new CBonuses(_pRECT->left,_pRECT->top,_quadTree,1,_pSoundManager);
					_quadTree->Add(_quadTree->_pRoot,objTemp);
				}
				else
				{
					objTemp=new CBonuses( _pRECT->left,_pRECT->top,_quadTree,2,_pSoundManager);
					_quadTree->Add(_quadTree->_pRoot,objTemp);
				}
				break;
			case 2://Extralife
				_pSoundManager->Play("BonusAppear_LevelDown",false);
				objTemp=new CBonuses(_pRECT->left,_pRECT->top,_quadTree,3,_pSoundManager);
				_quadTree->Add(_quadTree->_pRoot,objTemp);
				break;
			case 3://Star
				_pSoundManager->Play("BonusAppear_LevelDown",false);
				objTemp=new CBonuses(_pRECT->left,_pRECT->top,_quadTree,4,_pSoundManager);
				_quadTree->Add(_quadTree->_pRoot,objTemp);
				break;
			case 4://poision Mushroom
				_pSoundManager->Play("BonusAppear_LevelDown",false);
				objTemp=new CBonuses(_pRECT->left,_pRECT->top,_quadTree,6,_pSoundManager);
				_quadTree->Add(_quadTree->_pRoot,objTemp);
				break;
			}
			_kind=5;
			_hidden=-1;
			break;
		case 3://gach co nhieu coin
			_amountCoin--;
			if(_amountCoin<0)
			{
				_pSprite->_index=1;
				_pSprite->_row=0;
				_pSprite->_interval=0;
				_hidden-1;
				_kind=5;
			}
			else
			{
				_effect->Add(_pRECT->left,_pRECT->top-ITEMS_H-40,1);
				_pSoundManager->Play("GetCoin",false);
				info->CointCounter++;
			}
			break;
		}
		if(_check){
			_deltaY=15;
			//_posOld=_pRECT->top;	
			//_pRECT->top-=_deltaY;
			//_pRECT->bottom-=_deltaY;
			_posOld=_pSprite->_top;
			_pSprite->_top-=_deltaY;
			_check=false;
		}
	}
}
void CItem::Draw(int VirtualLeft,int Style)
{
	//if(_pRECT->top<_posOld||_deltaY>0)
	if(_deltaY>0)
	{
		_deltaY--;
		//_pRECT->top++;
		//_pRECT->bottom++;
		_pSprite->_top++;
	}
	if (_pSprite->_top==_posOld)
	{
		_check=true;
	}
	_pSprite->Draw(VirtualLeft,0,true);
}
int CItem::Move()
{
	return 0;
}

CItem::~CItem(void)
{
}
